var COS = require('../../../utils/cos-wx-sdk-v5')

Page({
    data: {
      userInfo: {},
      userName: '',
      phone: '',
      userAvatar:'https://ov-library-1316830564.cos.ap-guangzhou.myqcloud.com/userAvatar/',
      avatarImage: ''
    },
  
    onLoad() {
      const user = wx.getStorageSync('userInfo');
 
      this.setData({
        userInfo: user,
        userName: user.userName || '',
        phone: user.phone || ''
      });
      
      console.log("userInfo:", this.data.userInfo);

    },
  
    onUserNameInput(e) {
      this.setData({ userName: e.detail.value });
    },
  
    onPhoneInput(e) {
      this.setData({ phone: e.detail.value });
    },
  
    onSubmit() {
      const { userName, phone, userInfo } = this.data;
      if (!userName.trim() || !phone.trim()) {
        wx.showToast({ title: '请填写完整信息', icon: 'none' });
        return;
      }
      if (!/^1[3456789]\d{9}$/.test(phone)) {
        wx.showToast({
            title: '请输入正确的手机号',
            icon: 'none'
        });
        return;
    }
  
      wx.request({
        url: 'http://localhost:8080/user/updateUserInfo',
        method: 'POST',
        data: {
          userId: userInfo.userId, // 根据你登录的唯一标识
          userName,
          phone
        },
        success: res => {
          if (res.data.code === 0) {
            wx.showToast({ title: '修改成功' });
            userInfo.userName = userName;
            userInfo.phone = phone;
            wx.setStorageSync('userInfo', userInfo);
            getApp().globalData.userInfo = userInfo;
            // setTimeout(() => wx.navigateBack(), 1000);

            var pages = getCurrentPages();//获取页面栈
            if (pages.length > 1) {
              //上一个页面实例对象
              var prePage = pages[pages.length - 2];
              //调用上一个页面的onShow方法
              prePage.onShow()
            }
            wx.navigateBack({
              delta: 1
            })

          } else {
            wx.showToast({ title: '修改失败', icon: 'none' });
          }
        }
      });
    },

    getPhoneNumber: function (e) {
        var that = this
        // 一定是getPhoneNumber里面的code
        const code = e.detail.code
        wx.login({
          success: res => {
            if (res.code) {
              wx.getUserInfo({
                success: res1 => {
                  wx.request({
                    url: 'http://localhost:8080/user/getPhoneNumber',
                    method: 'POST',
                    data: {
                      code: code,
                      encryptedData: res1.encryptedData,
                      iv: res1.iv
                    },
                    success: res => {
                      that.setData({
                        phone: res.data.phoneNumber
                      })
                    }
                  })
                }
              })
            } else {
              console.log('获取失败！' + res.errMsg)
            }
          }
        })
      },

      uploadImage() {
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            success: res => {
                const path = res.tempFiles[0].tempFilePath;
                this.setData({
                    avatarImage: path
                });
                this.uploadIng();
            }
        });
    },

    uploadIng() {
        var cos = new COS({
            SecretId: 'AKIDtmUzHNGySikbMuGOM6f1B7qMeqpa9VdY',
            SecretKey: 'mJfnjQvDI9mESVK0VotkPr56vZ0EF4g4',
            SimpleUploadMethod: 'putObject',
        })

        var filePath = this.data.avatarImage
        console.log(this.data.userInfo.userId)
        cos.postObject({
            Bucket: 'ov-library-1316830564', //对象储存桶的名称
            Region: 'ap-guangzhou', //所属地域
            Key: 'userAvatar/' + this.data.userInfo.userId + '.jpg', // 存储在桶里的对象键
            FilePath: filePath,
        }, function (err, data) {
            
            if (err) {
                console.log('上传失败', err);
            } else {
                console.log('上传成功', data);
            }
        })
    }

  });
  